package edu.kit.kitcampusguide.poimanager.persistence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import edu.kit.kitcampusguide.poimanager.model.POI;
import edu.kit.kitcampusguide.poimanager.model.POITestData;

/**
 * 
 * This Class acts as a Gateway to the POI-Data, it uses a Singleton-Pattern. 
 * It containing the basic CRUD and search functions.
 * No Database-Connection is established, the POI-Objects are only saved in memory as an ArrayList<POI>.
 *  
 * 
 * * @author Robert Reutter
 */
public class POIGateway {

	private static POIGateway instance = null;
	Collection<POI> poiData = new ArrayList<POI>();
	
	
	protected POIGateway() {
		//generating Testdata
	      this.poiData = POITestData.generatePOIList();
	   }
	

	//Singleton Static Method, return Database instance 
	public static POIGateway getInstance() {
	      if(instance == null) {
	    	  //Create Database
	         instance = new POIGateway();
	      }
	      return instance;
	   }
	
	
	//adding POI Object to Collection<POI> poiData	
	public void createPOI(int id, String name, String category, Double latitude, Double longitude)
	{
		//create new POI-Object
		POI newPOI = new POI(id, name, category, latitude, longitude);
		//add POI to Database(ArrayList)
		this.poiData.add(newPOI);
	}
	
	
	//searching for POI-ID, returning the first found/matching POI-Object
	public POI readPOI(int poiID)
	{
		try {
		
		for (POI poiDataObject : poiData) 
		 if(poiDataObject.getId() == poiID)
				{
					return poiDataObject;
				}
			
		} catch (NullPointerException e) 
			{
				// TODO: handle exception
			}
		//TODO add: throw not such id Exception, null pointer
		return null;
	} //readPOI
	
	
	//Updating the POI-Object in the poiData 
	public void updatePOI(int id, String name, String category, Double latitude, Double longitude)
	{
		//iterate over poiData Collection
		for (POI poiDataObject : poiData) 
		{
			//search for already saved POI-Object with this ID
			if(poiDataObject.getId() == id)
				{
					poiDataObject.setName(name);
					poiDataObject.setCategory(category);
					poiDataObject.setLatitude(latitude);
					poiDataObject.setLongitude(longitude);
				}
		}
	} // updatePOI
	
	
	//removing POI-Object from the poiData Collection
	public void deletePOI(POI poi)
	{
		poiData.remove(poi);
	}
	
	
	public void deletePOI(int poiId)
	{
		POI searchObject = null;
		for (POI poiDataObject : poiData) 
			 if(poiDataObject.getId() == poiId)
					{
				 		searchObject = poiDataObject;
					}
		try {
		poiData.remove(searchObject);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
				
	
	//returning the poiData Object, Collection<POI>
	public Collection<POI> getAllPOIs()
	{
		return poiData;
	}
	
	
	// return a sublist (filtered Collection) of poiData 
	//containing all matching POIs with this category String
	public Collection<POI> searchByCategory(String category)
	{
		//create new Collection for filtered POIs
		Collection<POI> poiSearchData = new ArrayList<POI>();
		
		//for all POIs in the Database
		for (POI poiDataObject : poiData)
		{
			//if the POI 'category' String contains the 'category search string'
			//example: if .getCategory()="essen" and search parameter category="ess", then this will be true
			if(poiDataObject.getCategory().indexOf(category) != -1)
			{
				//adding current POI from database to filter list
				poiSearchData.add(poiDataObject);
			}
		}
		return poiSearchData;
	}
	
	
	//adding all category strings from database to a List<String>
	//can be used when user creates new POI, for category proposal
	public List<String> getAllCategories()
	{
		List<String> categories = new ArrayList<String>();
		
		for (POI poiDataObject : poiData)
		{
			//add Category of poiDataObject to category searchList
			categories.add(poiDataObject.getCategory());
		}
		
		return categories;
	}
	
} // class
